﻿using System;

namespace CommonFramework.FixMath
{
    public class FRandom
    {
        public ulong RandSeed = 1;

        public FRandom(uint seed = 17)
        {
            RandSeed = seed;
        }
        public FFloat Value => FFloat.FromRawInt(Range(0, 1000));

        public uint Next()
        {
            RandSeed = RandSeed * 1103515245 + 36153;
            return (uint)(RandSeed / 65536);
        }
        // range:[0 ~(max-1)]
        public uint Next(uint max)
        {
            return Next() % max;
        }
        public int Next(int max)
        {
            return (int)(Next() % max);
        }
        // range:[min~(max-1)]
        public uint Range(uint min, uint max)
        {
            if (min > max)
                throw new ArgumentOutOfRangeException("minValue",
                    string.Format("'{0}' cannot be greater than {1}.", min, max));

            uint num = max - min;
            return this.Next(num) + min;
        }
        public int Range(int min, int max)
        {
            if (min >= max - 1)
                return min;
            int num = max - min;

            return this.Next(num) + min;
        }

        public FFloat Range(FFloat min, FFloat max)
        {
            if (min > max)
                throw new ArgumentOutOfRangeException("minValue",
                    string.Format("'{0}' cannot be greater than {1}.", min, max));

            uint num = (uint)(max.RawVal() - min.RawVal());
            return FFloat.FromRawLong((long)Next(num) + min.RawVal());
        }
    }
}